/*
 * @Author: zhangfeng
 * @Date: 2023-12-29 17:04:26
 * @LastEditTime: 2023-12-29 17:48:21
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \nodeapi\src\middleware\role-premission.middleware.js
 * © 2023 张峰，All rights reserved.
 */
const { getRolePremissionServer } = require("../server/role-premission.server");
const { getMenusById } = require("../server/menus.server");
const { rolePremissionGetFromatterError } = require("../constants/err.type");
const getPremissionByRoleId = async (ctx, next) => {
  try {
    const res = await getRolePremissionServer(ctx.state.user.roleIds);
    let premissionIds = res.map((v) => v.premissionId);
    const result = await getMenusById(premissionIds);
    ctx.state.user.menus = result;
  } catch (error) {
    console.error(error);
    ctx.body = rolePremissionGetFromatterError;
  }
  await next();
};

module.exports = {
  getPremissionByRoleId,
};
